﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerStuReviewDao : IStuReviewDao
    {
        public SqlServerStuReviewDao()
        {
            SQLUtility.TableName = "StuReview";
        }

        //Trả về đối tượng báo ăn
        private StuReview MakeStuReview(DataRow row)
        {
            Guid _id = Guid.Parse(row["StuReviewID"].ToString());
            Guid _studentID = Guid.Parse(row["StudentID"].ToString());
            Guid _schoolYearID = Guid.Parse(row["SchoolYearID"].ToString());
            Guid _classID = Guid.Parse(row["ClassID"].ToString());
            bool _theChatDauNam = bool.Parse(row["TheChatDauNam"].ToString());
            bool _theChatCuoiNam = bool.Parse(row["TheChatCuoiNam"].ToString());
            bool _nhanThucDauNam = bool.Parse(row["NhanThucDauNam   "].ToString());
            string _nhanThucCuoiNam = row["NhanThucCuoiNam"].ToString();
            bool _ngonNguDauNam = bool.Parse(row["NgonNguDauNam"].ToString());
            string _ngonNguCuoiNam = row["NgonNguCuoiNam"].ToString();
            bool _qHXHDauNam = bool.Parse(row["QHXHDauNam"].ToString());
            bool _qHXHCuoiNam = bool.Parse(row["QHXHCuoiNam"].ToString());
            bool _tongHop = bool.Parse(row["TongHop"].ToString());
            return new StuReview(_id, _studentID, _schoolYearID, _classID, _theChatDauNam, _theChatCuoiNam, _nhanThucDauNam, _nhanThucCuoiNam, _ngonNguDauNam, _ngonNguCuoiNam, _qHXHDauNam, _qHXHCuoiNam, _tongHop);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<StuReview> MakeStuReviews(DataTable dt)
        {
            IList<StuReview> list = new List<StuReview>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeStuReview(row));
            }
            return list;
        }
        private DbParameter[] Take(StuReview StuReview)
        {
            DbParameter[] parameters = new DbParameter[13];
            parameters[0] = new SqlParameter("@StuReviewID", StuReview.Id);
            parameters[1] = new SqlParameter("@StudentID", StuReview.StudentID);
            parameters[2] = new SqlParameter("@SchoolYearID", StuReview.SchoolYearID);
            parameters[3] = new SqlParameter("@ClassID", StuReview.ClassID);
            parameters[4] = new SqlParameter("@TheChatDauNam", StuReview.TheChatDauNam);

            parameters[5] = new SqlParameter("@TheChatCuoiNam", StuReview.TheChatCuoiNam);
            parameters[6] = new SqlParameter("@NhanThucDauNam", StuReview.NhanThucDauNam);
            parameters[7] = new SqlParameter("@NhanThucCuoiNam", StuReview.NhanThucCuoiNam);
            parameters[8] = new SqlParameter("@NgonNguDauNam", StuReview.NgonNguDauNam);
            parameters[9] = new SqlParameter("@NgonNguCuoiNam", StuReview.NgonNguCuoiNam);

            parameters[10] = new SqlParameter("@QHXHDauNam", StuReview.QHXHDauNam);
            parameters[11] = new SqlParameter("@QHXHCuoiNam", StuReview.QHXHCuoiNam);
            parameters[12] = new SqlParameter("@TongHop", StuReview.TongHop);
            return parameters;
        }
        public IList<Business_Objects.StuReview> GetStuReview()
        {
            SQLUtility.TableName = "StuReview";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeStuReviews(dt);
        }

        public Business_Objects.StuReview GetStuReviewByID(string StuReviewID)
        {
            SQLUtility.TableName = "StuReview";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@StuReviewID", StuReviewID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeStuReview(row);
            }
        }

        public void InsertStuReview(Business_Objects.StuReview StuReview)
        {
            SQLUtility.TableName = "StuReview";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(StuReview), true);
        }

        public int UpdateStuReview(Business_Objects.StuReview StuReview)
        {
            SQLUtility.TableName = "StuReview";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(StuReview)); }
            catch { return -1; }
        }

        public int DeleteStuReview(Business_Objects.StuReview StuReview)
        {
            SQLUtility.TableName = "StuReview";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(StuReview)); }
            catch { return -1; }
        }
    }
}
